<?php
/* 	These code was generated using phpCIGen v 0.1.b (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,

	+ Module  		: t_order Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_t_order.php
 	+ Author  		: 
 	+ Created on 05/Mar/2012 14:57:56

*/

//class of t_order
class C_t_order extends CI_Controller {

	//constructor
	function C_t_order(){
		parent::__construct();
		session_start();
		$this->load->model('m_t_order', '', TRUE);
	}

	//set index
	function index(){
		$this->load->helper('to_excel');
		$this->load->view('main/v_t_order');
	}

	//for detail action
	//list detail handler action
	function  detail_t_orderdet_list(){
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$master_id = (integer) (isset($_POST['master_id']) ? @$_POST['master_id'] : @$_GET['master_id']);
		$result=$this->m_t_order->detail_t_orderdet_list($master_id,$query,$start,$end);
		echo $result;
	}
	//end of handler

	//add detail
	function detail_t_orderdet_insert($master_id){
	//POST variable here
		//auto increment, don't accept anything from form values
		$t_order_id=$master_id;
                $orderdet_id=trim(@$_POST["orderdet_id"]);
		$orderdet_detail=@$_POST["orderdet_detail"];
                $orderdet_detail=str_replace("/(<\/?)(p)([^>]*>)", "",$orderdet_detail);
                $orderdet_detail=str_replace("'", "''",$orderdet_detail);
		$orderdet_qty=@$_POST["orderdet_qty"];
		$orderdet_price=@$_POST["orderdet_price"];

		$orderdet_id = json_decode($orderdet_id);
		$orderdet_detail = json_decode($orderdet_detail);
		$orderdet_qty = json_decode($orderdet_qty);
		$orderdet_price = json_decode($orderdet_price);

		$result=$this->m_t_order->detail_t_orderdet_insert($orderdet_id ,$t_order_id ,$orderdet_detail ,$orderdet_qty ,$orderdet_price );
		return $result;
	}


	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->t_order_list();
				break;
			case "UPDATE":
				$this->t_order_update();
				break;
			case "CREATE":
				$this->t_order_create();
				break;
			case "DELETE":
				$this->t_order_delete();
				break;
			case "SEARCH":
				$this->t_order_search();
				break;
			case "PRINT":
				$this->t_order_print();
				break;
			case "EXCEL":
				$this->t_order_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}

//function fot list record
	function t_order_list(){

		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_t_order->t_order_list($query,$start,$end);
		echo $result;
	}

	//function for create new record
	function t_order_create(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$order_code=trim(@$_POST["order_code"]);
		$order_code=htmlentities($order_code,ENT_QUOTES);
		$order_date=trim(@$_POST["order_date"]);
		$order_duedate=trim(@$_POST["order_duedate"]);
		$m_customer_id=@$_POST["m_customer_id"];
		$m_customer_id=is_numeric($m_customer_id)?$m_customer_id:0;
		$m_staff_id=@$_POST["m_staff_id"];
		$m_staff_id=is_numeric($m_staff_id)?$m_staff_id:0;
		$order_stat=trim(@$_POST["order_stat"]);
		$order_stat=htmlentities($order_stat,ENT_QUOTES);
		$order_note=trim(@$_POST["order_note"]);
		$order_note=htmlentities($order_note,ENT_QUOTES);
		$result=$this->m_t_order->t_order_create($order_code ,$order_date ,$order_duedate ,$m_customer_id ,$m_staff_id ,$order_stat ,$order_note );
		$result=$this->detail_t_orderdet_insert($result);
		echo $result;
	}


	//function for update record
	function t_order_update(){
		//POST variable here
		$order_id=@$_POST["order_id"];
		$order_id=is_numeric($order_id)?$order_id:0;
		$order_code=trim(@$_POST["order_code"]);
		$order_code=htmlentities($order_code,ENT_QUOTES);
		$order_date=trim(@$_POST["order_date"]);
		$order_duedate=trim(@$_POST["order_duedate"]);
		$m_customer_id=@$_POST["m_customer_id"];
		$m_customer_id=is_numeric($m_customer_id)?$m_customer_id:0;
		$m_staff_id=@$_POST["m_staff_id"];
		$m_staff_id=is_numeric($m_staff_id)?$m_staff_id:0;
		$order_stat=trim(@$_POST["order_stat"]);
		$order_stat=htmlentities($order_stat,ENT_QUOTES);
		$order_note=trim(@$_POST["order_note"]);
		$order_note=htmlentities($order_note,ENT_QUOTES);
		$result = $this->m_t_order->t_order_update($order_id,$order_code,$order_date,$order_duedate,$m_customer_id,$m_staff_id,$order_stat,$order_note);
		$result=$this->detail_t_orderdet_insert($result);
		echo $result;
	}

	//function for delete selected record
	function t_order_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_t_order->t_order_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function t_order_search(){
		//POST varibale here
		$order_id=@$_POST["order_id"];
		$order_id=is_numeric($order_id)?$order_id:0;
		$order_code=trim(@$_POST["order_code"]);
		$order_code=htmlentities($order_code,ENT_QUOTES);
		$order_date=trim(@$_POST["order_date"]);
		$order_duedate=trim(@$_POST["order_duedate"]);
		$m_customer_id=@$_POST["m_customer_id"];
		$m_customer_id=is_numeric($m_customer_id)?$m_customer_id:0;
		$m_staff_id=@$_POST["m_staff_id"];
		$m_staff_id=is_numeric($m_staff_id)?$m_staff_id:0;
		$order_stat=trim(@$_POST["order_stat"]);
		$order_stat=htmlentities($order_stat,ENT_QUOTES);
		$order_note=trim(@$_POST["order_note"]);
		$order_note=htmlentities($order_note,ENT_QUOTES);

		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_t_order->t_order_search($order_id ,$order_code ,$order_date ,$order_duedate ,$m_customer_id ,$m_staff_id ,$order_stat ,$order_note ,$start,$end);
		echo $result;
	}


	function t_order_print(){
  		//POST varibale here
		$order_id=@$_POST["order_id"];
		$order_id=is_numeric($order_id)?$order_id:0;
		$order_code=trim(@$_POST["order_code"]);
		$order_code=htmlentities($order_code,ENT_QUOTES);
		$order_date=trim(@$_POST["order_date"]);
		$order_duedate=trim(@$_POST["order_duedate"]);
		$m_customer_id=@$_POST["m_customer_id"];
		$m_customer_id=is_numeric($m_customer_id)?$m_customer_id:0;
		$m_staff_id=@$_POST["m_staff_id"];
		$m_staff_id=is_numeric($m_staff_id)?$m_staff_id:0;
		$order_stat=trim(@$_POST["order_stat"]);
		$order_stat=htmlentities($order_stat,ENT_QUOTES);
		$order_note=trim(@$_POST["order_note"]);
		$order_note=htmlentities($order_note,ENT_QUOTES);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];

		$data["data_print"] = $this->m_t_order->t_order_print($order_id ,$order_code ,$order_date ,$order_duedate ,$m_customer_id ,$m_staff_id ,$order_stat ,$order_note ,$option,$filter);
		$print_view=$this->load->view("main/p_t_order.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/t_order_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function t_order_export_excel(){
		//POST varibale here
		$order_id=@$_POST["order_id"];
		$order_id=is_numeric($order_id)?$order_id:0;
		$order_code=trim(@$_POST["order_code"]);
		$order_code=htmlentities($order_code,ENT_QUOTES);
		$order_date=trim(@$_POST["order_date"]);
		$order_duedate=trim(@$_POST["order_duedate"]);
		$m_customer_id=@$_POST["m_customer_id"];
		$m_customer_id=is_numeric($m_customer_id)?$m_customer_id:0;
		$m_staff_id=@$_POST["m_staff_id"];
		$m_staff_id=is_numeric($m_staff_id)?$m_staff_id:0;
		$order_stat=trim(@$_POST["order_stat"]);
		$order_stat=htmlentities($order_stat,ENT_QUOTES);
		$order_note=trim(@$_POST["order_note"]);
		$order_note=htmlentities($order_note,ENT_QUOTES);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];

		$query = $this->m_t_order->t_order_export_excel($order_id ,$order_code ,$order_date ,$order_duedate ,$m_customer_id ,$m_staff_id ,$order_stat ,$order_note ,$option,$filter);

		to_excel($query,"t_order");
		echo '1';

	}

	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}

	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}


}
?>